package br.gov.df.emater.web.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.component.tabview.TabView;
import org.primefaces.event.TabChangeEvent;
import org.primefaces.event.TabCloseEvent;

@ManagedBean
@SessionScoped
public class TabPrincipalMB implements Serializable {

	public class TabItem implements Serializable {

		private static final long serialVersionUID = -4015492797123919523L;

		private boolean closable;
		private String name;
		private int tabIndex;
		private String url;

		public TabItem(String name, String url, int tabIndex, boolean closable) {
			this.setName(name);
			this.setUrl(url);
			this.setTabIndex(tabIndex);
			this.setClosable(closable);
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			TabItem other = (TabItem) obj;
			if (!getOuterType().equals(other.getOuterType()))
				return false;
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			return true;
		}

		// TODO assim que necessário colocar mais atributos nessa tab

		public String getName() {
			return name;
		}

		private TabPrincipalMB getOuterType() {
			return TabPrincipalMB.this;
		}

		public int getTabIndex() {
			return tabIndex;
		}

		public String getUrl() {
			return url;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			return result;
		}

		public boolean isClosable() {
			return closable;
		}

		public void setClosable(boolean closeable) {
			this.closable = closeable;
		}

		public void setName(String name) {
			this.name = name;
		}

		public void setTabIndex(int tabIndex) {
			this.tabIndex = tabIndex;
		}

		public void setUrl(String url) {
			this.url = url;
		}
	}

	private static final long serialVersionUID = -5317320425869773387L;

	private int activeIndex = 0;

	private List<TabItem> tabList = new ArrayList<TabItem>();

	public TabPrincipalMB() {
		addTab(new TabItem("Bem-Vindo", "../pages/boasVindas.xhtml",
				getTabList().size(), false));
	}

	public void addTab() {
		addTab(new TabItem("Teste", "page1.xhtml", tabList.size(), true));
	}

	public void addTab(TabItem tabItem) {
		int newActiveIndex = getTabList().size();

		getTabList().add(tabItem);

		// define activeIndex com -1 porque inicia em 0
		setActiveIndex(newActiveIndex);
	}

	public String exibeMapa() {
		return "https://maps.google.com.br/maps?q=EMATER-DF";
	}

	public void exibePessoa() {
		addTab(new TabItem("Cadastro de Pessoas", "pessoas/default.xhtml",
				tabList.size(), true));
	}

	public int getActiveIndex() {
		return activeIndex;
	}

	public List<TabItem> getTabList() {
		return tabList;
	}

	public void onTabChange(TabChangeEvent event) {
		if (event != null && event.getTab() != null
				&& event.getTab().getTitle() != null)
			System.out.println(event.getTab().getTitle());
	}

	public void onTabClose(TabCloseEvent event) {
		TabView tb = (TabView) event.getComponent();
		FacesMessage msg = new FacesMessage("Tab Fechada", "Fechada Tab: "
				+ tb.getActiveIndex());
		System.out.printf("Tab fechada [%d]", tb.getActiveIndex());
		FacesContext.getCurrentInstance().addMessage(null, msg);
		getTabList().remove(tb.getActiveIndex());
		activeIndex--;
	}

	public void setActiveIndex(int activeIndex) {
		this.activeIndex = activeIndex;
	}

	public void setTabList(List<TabItem> tabList) {
		this.tabList = tabList;
	}

	public String login() {
		return "/pages/login";
	}
}
